home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / delphi1 / lesson2 / vatform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-12-11  |  2.1 KB  |  101 lines

  1. unit Vatform;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, VatCalc, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     CalcBtn: TButton;
  12.     GroupBox1: TGroupBox;
  13.     SubTotRB: TRadioButton;
  14.     GrandTotRB: TRadioButton;
  15.     SubTotal: TEdit;
  16.     Vat: TEdit;
  17.     GrandTotal: TEdit;
  18.     Label1: TLabel;
  19.     procedure CalcBtnClick(Sender: TObject);
  20.     procedure SubTotRBClick(Sender: TObject);
  21.     procedure GrandTotRBClick(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. procedure InputError( TE: TEdit; errcode : integer );
  36. var
  37.    Msg : string;
  38. begin
  39.     if TE.Text = '' Then
  40.        Msg := 'You must enter a value'
  41.     else
  42.        Msg :=  'Invalid character: ' + Copy(TE.Text, errcode, 1);
  43.     MessageDlg(Msg, mtError,
  44.             [mbOk], 0);
  45.     TE.SetFocus;
  46.     TE.SelStart := errcode-1;
  47.     TE.SelLength := 1;
  48. end;
  49.  
  50. procedure TForm1.CalcBtnClick(Sender: TObject);
  51. var
  52.    stxt, vtxt,
  53.        gtxt : string;
  54.    st, vt, gt  : real;
  55.    errcode : integer;
  56. begin
  57.    st := 0.0;
  58.    vt := 0.0;
  59.    gt := 0.0;
  60.    stxt := '';
  61.    vtxt := '';
  62.    gtxt := '';
  63.    if SubTotRB.Checked Then
  64.    begin
  65.        Val(SubTotal.Text, st, errcode);
  66.        if errcode <> 0 then InputError(SubTotal,
  67.            errcode);
  68.        begin
  69.           PlusVat(st,vt,gt);
  70.           Str(vt:2:2, vtxt );
  71.           Str(gt:2:2, gtxt );
  72.           Vat.Text := vtxt;
  73.           GrandTotal.Text := gtxt;
  74.        end
  75.    end
  76.    else
  77.    begin
  78.        Val(GrandTotal.Text, gt, errcode);
  79.        if errcode <> 0 then InputError( GrandTotal, errcode );
  80.        begin
  81.           MinusVat(st,vt,gt);
  82.           Str(vt:2:2, vtxt );
  83.           Str(st:2:2, stxt );
  84.           Vat.Text := vtxt;
  85.           SubTotal.Text := stxt; 
  86.        end
  87.    end
  88. end;
  89.  
  90. procedure TForm1.SubTotRBClick(Sender: TObject);
  91. begin
  92.      CalcBtn.Caption := '&Calculate the Grand Total';
  93. end;
  94.  
  95. procedure TForm1.GrandTotRBClick(Sender: TObject);
  96. begin
  97.      CalcBtn.Caption := '&Calculate the Sub Total';
  98. end;
  99.  
  100. end.
  101.